package com.battery.web.controller.system.battery;

import java.util.List;

import com.battery.common.utils.DateUtils;
import com.battery.system.domain.BO.PointsConvertBO;
import com.battery.system.domain.VO.PointsConvertVO;
import com.battery.system.service.battery.IPointsConvertService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.battery.common.annotation.Log;
import com.battery.common.enums.BusinessType;
import com.battery.system.domain.PointsConvert;
import com.battery.common.core.controller.BaseController;
import com.battery.common.core.domain.AjaxResult;
import com.battery.common.utils.poi.ExcelUtil;
import com.battery.common.core.page.TableDataInfo;

/**
 * 积分兑换订单Controller
 *
 * @author battery
 * @date 2021-03-24
 */
@Controller
@RequestMapping("/system/pointsConvert")
public class PointsConvertController extends BaseController {
    private String prefix = "system/pointsConvert";

    @Autowired
    private IPointsConvertService pointsConvertService;

    @RequiresPermissions("system:pointsConvert:view")
    @GetMapping()
    public String pointsConvert() {
        return prefix + "/pointsConvert";
    }

    /**
     * 查询积分兑换订单列表
     */
    @RequiresPermissions("system:pointsConvert:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PointsConvertBO pointsConvertBO) {
        startPage();
//        List<PointsConvert> list = pointsConvertService.selectPointsConvertList(pointsConvert);
        List<PointsConvertVO> list = pointsConvertService.selectPointsConvertVOList(pointsConvertBO);
        return getDataTable(list);
    }

    /**
     * 导出积分兑换订单列表
     */
    @RequiresPermissions("system:pointsConvert:export")
    @Log(title = "积分兑换订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PointsConvert pointsConvert) {
        List<PointsConvert> list = pointsConvertService.selectPointsConvertList(pointsConvert);
        ExcelUtil<PointsConvert> util = new ExcelUtil<PointsConvert>(PointsConvert.class);
        return util.exportExcel(list, "pointsConvert");
    }

    /**
     * 新增积分兑换订单
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存积分兑换订单
     */
    @RequiresPermissions("system:pointsConvert:add")
    @Log(title = "积分兑换订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PointsConvert pointsConvert) {
        return toAjax(pointsConvertService.insertPointsConvert(pointsConvert));
    }

    /**
     * 修改积分兑换订单
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        PointsConvert pointsConvert = pointsConvertService.selectPointsConvertById(id);
        mmap.put("pointsConvert", pointsConvert);
        return prefix + "/edit";
    }

    /**
     * 修改保存积分兑换订单
     */
    @RequiresPermissions("system:pointsConvert:edit")
    @Log(title = "积分兑换订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PointsConvert pointsConvert) {
        return toAjax(pointsConvertService.updatePointsConvert(pointsConvert));
    }

    /**
     * 删除积分兑换订单
     */
    @RequiresPermissions("system:pointsConvert:remove")
    @Log(title = "积分兑换订单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(pointsConvertService.deletePointsConvertByIds(ids));
    }


    /**
     * 展示积分兑换对应的商品列表
     *
     * @param pointsConvertBO
     * @return
     */
    @RequiresPermissions("system:pointsConvert:list")
    @PostMapping("/itemslist")
    @ResponseBody
    public TableDataInfo itemslist(PointsConvertBO pointsConvertBO) {
        startPage();
        List<PointsConvertVO> list = pointsConvertService.selectPointsConvertItemsVOList(pointsConvertBO);
        return getDataTable(list);
    }


    /**
     * 积分兑换审核通过
     *
     * @param id
     * @return
     */
    @RequiresPermissions("system:pointsConvert:passOrNo")
    @Log(title = "机柜兑换审核通过", businessType = BusinessType.OTHER)
    @PostMapping("/pass")
    @ResponseBody
    public AjaxResult pass(String id) {
        if (StringUtils.isBlank(id)) {
            return error("主键ID不能为空");
        }

        PointsConvert pointsConvertUpdate = new PointsConvert();
        pointsConvertUpdate.setId(id);
        pointsConvertUpdate.setApprovalTime(DateUtils.getNowDate());
        pointsConvertUpdate.setStatus(1L);
        return toAjax(pointsConvertService.updateByPrimaryKeySelective(pointsConvertUpdate));
    }


    @RequiresPermissions("system:pointsConvert:passOrNo")
    @Log(title = "机柜兑换拒绝审核", businessType = BusinessType.OTHER)
    @PostMapping("/nopass")
    @ResponseBody
    public AjaxResult nopass(String id) {
        if (StringUtils.isBlank(id)) {
            return error("主键ID不能为空");
        }

        return toAjax(pointsConvertService.nopass(id));
    }


    @RequiresPermissions("system:pointsConvert:deliver")
    @Log(title = "机柜兑换发货", businessType = BusinessType.OTHER)
    @PostMapping("/deliver")
    @ResponseBody
    public AjaxResult deliver(String id) {
        if (StringUtils.isBlank(id)) {
            return error("主键ID不能为空");
        }
        PointsConvert pointsConvertUpdate = new PointsConvert();
        pointsConvertUpdate.setId(id);
        pointsConvertUpdate.setDeliveryTime(DateUtils.getNowDate());
        pointsConvertUpdate.setStatus(2L);
        return toAjax(pointsConvertService.updateByPrimaryKeySelective(pointsConvertUpdate));
    }
}
